package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author fly
 * @since 2021-09-12
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation("讲师列表")
    @GetMapping("/findAll")
    public R findAllTeacher() {
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            throw new GuliException(20001, "自定义业务异常");
        }
        return R.ok().data("items", eduTeacherService.list(null));
    }

    /**
     * 逻辑删除
     */
    @ApiOperation("根据id删除讲师")
    @DeleteMapping("/{id}")
    public R removeById(
            @ApiParam(name = "id", value = "讲师id", required = true)
            @PathVariable String id) {
        boolean flag = eduTeacherService.removeById(id);
        return flag ? R.ok() : R.error();
    }

    @ApiOperation("分页查询讲师")
    @GetMapping("/page/{current}/{size}")
    public R page(@PathVariable Long current, @PathVariable Long size) {
        IPage<EduTeacher> page = new Page<>(current, size);
        eduTeacherService.page(page, null);
        return R.ok().data("total", page.getTotal()).data("rows", page.getRecords());
    }

    @ApiOperation(value = "多条件查询讲师带分页")
    @PostMapping("/pageCondition/{current}/{size}")
    public R pageCondition(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable Long current,
                           @ApiParam(name = "size", value = "每页记录数", required = true) @PathVariable Long size,
                           @RequestBody(required = false) TeacherQuery teacherQuery) {
        //判断条件值是否为空，如果不为空，拼接条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        // 按添加时间降序排序
        wrapper.orderByDesc("gmt_modified");
        if (!Objects.isNull(teacherQuery)) {
            //通过封装TeacherQuery对象来直接传递查询条件
            String name = teacherQuery.getName();
            Integer level = teacherQuery.getLevel();
            String begin = teacherQuery.getBegin();
            String end = teacherQuery.getEnd();

            if (!StringUtils.isEmpty(name)) {
                wrapper.like("name", name);
            }
            //判断是否传入教师头衔
            if (!StringUtils.isEmpty(level)) {
                wrapper.eq("level", level);
            }
            if (!StringUtils.isEmpty(begin)) {
                wrapper.ge("gmt_create", begin);
            }
            if (!StringUtils.isEmpty(begin)) {
                wrapper.le("gmt_modified", end);
            }
        }

        //创建分页page对象
        IPage<EduTeacher> page = new Page<>(current, size);
        //调用方法实现多条件分页查询
        eduTeacherService.page(page, wrapper);

        //获取查询到的数据
        List<EduTeacher> records = page.getRecords();
        //获取总记录数
        long total = page.getTotal();
        return R.ok().data("total", total).data("rows", records);

    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("/save")
    public R save(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.save(eduTeacher);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable String id) {
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("item", teacher);
    }

    @ApiOperation(value = "修改讲师")
    @PostMapping("/update")
    public R update(@RequestBody EduTeacher teacher) {
        boolean flag = eduTeacherService.updateById(teacher);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

